package org.example.ttms.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import org.example.ttms.customize.AddTicket;
import org.example.ttms.customize.Window;

import java.util.ArrayList;
import java.util.List;

public class AddTicketController {

    @FXML
    private Button okButton;

    @FXML
    private TextField buinessPriceTextField;

    @FXML
    private TextField firstPriceTextField;

    @FXML
    private TextField secondPriceTextField;

    @FXML
    private ChoiceBox<Integer> buinessChoiceBox;

    @FXML
    private ChoiceBox<Integer> firstChoiceBox;

    @FXML
    private ChoiceBox<Integer> secondChoiceBox;

    public static AddTicket ticket = new AddTicket();

    public void initialize(){
        //choiceBox
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i <= 15; i++){
            list.add(i);
        }
        buinessChoiceBox.getItems().addAll(list);
        buinessChoiceBox.getSelectionModel().select(ticket.getBusinessSeatNum());
        for(int i = 16; i <= 30; i++){
            list.add(i);
        }
        firstChoiceBox.getItems().addAll(list);
        firstChoiceBox.getSelectionModel().select(ticket.getFirstSeatNum());
        for(int i = 31; i <= 50; i++){
            list.add(i);
        }
        secondChoiceBox.getItems().addAll(list);
        secondChoiceBox.getSelectionModel().select(ticket.getSecondSeatNum());
        //textField
        if(ticket.getBusinessSeatPrice() != 0){
            buinessPriceTextField.setText(String.valueOf(ticket.getBusinessSeatPrice()));
        }
        if(ticket.getFirstSeatPrice() != 0){
            firstPriceTextField.setText(String.valueOf(ticket.getFirstSeatPrice()));
        }
        if(ticket.getSecondSeatPrice() != 0){
            secondPriceTextField.setText(String.valueOf(ticket.getSecondSeatPrice()));
        }
    }
    @FXML
    void chickOk(ActionEvent event) {
        //保存数据
        ticket.setBusinessSeatNum(buinessChoiceBox.getValue());
        ticket.setFirstSeatNum(firstChoiceBox.getValue());
        ticket.setSecondSeatNum(secondChoiceBox.getValue());
        ticket.setBusinessSeatPrice(buinessPriceTextField.getText().isEmpty() ? 0 : Double.parseDouble(buinessPriceTextField.getText()));
        ticket.setFirstSeatPrice(firstPriceTextField.getText().isEmpty() ? 0 : Double.parseDouble(firstPriceTextField.getText()));
        ticket.setSecondSeatPrice(secondPriceTextField.getText().isEmpty() ? 0 : Double.parseDouble(secondPriceTextField.getText()));
        ticket.setAllSeatNum(ticket.getBusinessSeatNum()+ ticket.getFirstSeatNum()+ ticket.getSecondSeatNum());
        //关闭窗口
        Window.stage.close();
    }

}
